import uvicorn
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware

from app.taobao import taobao
from app.zfb import zfb

app = FastAPI()

app.include_router(zfb)  # 支付宝
app.include_router(taobao)  # 淘宝

# 允许的域名
allowed_origins = [
    '*'
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=allowed_origins,  # 指定允许的域名
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有请求方法（GET, POST, PUT, DELETE, OPTIONS）
    allow_headers=["*"],  # 允许所有请求头
)

# if __name__ == '__main__':
#     uvicorn.run('main:app', host="0.0.0.0", port=8700, reload=True)

if __name__ == '__main__':
    import hypercorn.asyncio
    import asyncio
    from hypercorn.config import Config

    # 创建两个独立的 Config 实例，一个用于 HTTP，一个用于 HTTPS
    http_config = Config()
    http_config.bind = ["0.0.0.0:8700"]  # 监听 HTTP 端口
    http_config.reload = True  # 开发模式启用自动重载

    https_config = Config()
    https_config.bind = ["0.0.0.0:8800"]  # 监听 HTTPS 端口
    https_config.certfile = "./localhost.pem"  # 证书路径
    https_config.keyfile = "./localhost-key.pem"  # 私钥路径
    https_config.reload = True  # 开发模式启用自动重载

    https_442_config = Config()
    https_442_config.bind = ["0.0.0.0:442"]  # 监听 HTTPS 端口
    https_442_config.certfile = "./xiaogao.site-cert.pem"  # 证书路径
    https_442_config.keyfile = "./xiaogao.site-key.pem"  # 私钥路径
    https_442_config.reload = True  # 开发模式启用自动重载


    async def start_servers():
        await asyncio.gather(
            hypercorn.asyncio.serve(app, http_config),  # 启动 HTTP 服务器
            hypercorn.asyncio.serve(app, https_config),  # 启动 HTTPS 服务器
            hypercorn.asyncio.serve(app, https_442_config)  # 启动 HTTPS 服务器
        )


    asyncio.run(start_servers())
